package com.magic.gre.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.magic.gre.R;
import com.magic.gre.adapter.ActivityAdapter;
import com.magic.gre.base.fragment.BaseMVPFragment;
import com.magic.gre.entity.ActivityBean;
import com.magic.gre.helper.Contract;
import com.magic.gre.mvp.contract.AvtivityContract;
import com.magic.gre.mvp.presenter.AvtivityPresenterImpl;
import com.magic.gre.ui.activity.ActivityDetailsActivity;
import com.magic.gre.ui.activity.WebActivity;
import com.magic.gre.ui.dialog.AvtivityDialog;
import com.noname.lib_base_java.util.StatusBarUtil;
import com.noname.lib_base_java.util.ToastUtil;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Author: Lmoumou
 * Date: 2019-01-07 15:29
 * 文件名: AvtivityFragment
 * 描述:
 */
public class AvtivityFragment extends BaseMVPFragment<AvtivityPresenterImpl> implements AvtivityContract.View,
        ActivityAdapter.OnActivityitemListener, AvtivityDialog.OnActivityDialogListener {

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.status_bar_v)
    View statusView;

    public static AvtivityFragment newInstance() {
        return new AvtivityFragment();
    }

    private ActivityAdapter adapter;
    private List<ActivityBean> dataList = new ArrayList<>();
    private int pageNo;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_activity;
    }

    @Override
    protected AvtivityPresenterImpl creatPresenter() {
        return new AvtivityPresenterImpl(this);
    }

    @Override
    protected void lazyLoad() {
        pageNo = 1;
        mPresenter.pActivityList(pageNo);
    }

    @Override
    protected void netOverride() {
        lazyLoad();
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        super.initView(view, savedInstanceState);
        StatusBarUtil.setHeight(mContext, statusView);

        adapter = new ActivityAdapter(mContext, dataList,false);
        adapter.setOnActivityitemListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(adapter);
    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        super.onRefresh(refreshLayout);
        lazyLoad();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        super.onLoadMore(refreshLayout);
        mPresenter.pActivityList(pageNo);
    }

    @Override
    public void onItemClick(String url, String activityId) {
        mPresenter.pActDetails(activityId,"1");
    }


    @Override
    public void onSignUp(ActivityBean bean) {
        AvtivityDialog dialog = new AvtivityDialog();
        dialog.setOnActivityDialogListener(this);
        dialog.showThis(getChildFragmentManager(), AvtivityDialog.class.getSimpleName(), bean.getId(), bean.getApplyImage());
    }

    @Override
    public void vActivitylist(List<ActivityBean> beanList) {
        if (pageNo == 1) {
            dataList.clear();
            if (beanList == null || beanList.isEmpty()) {
                adapter.notifyDataSetChanged();
                stopRefresh(true, false);
                showEmpty();
                return;
            }
        }
        pageNo++;
        dataList.addAll(beanList);
        adapter.notifyDataSetChanged();
        stopRefresh(true, beanList.size() == Contract.PAGER_SIZE);
        showContent();
    }

    @Override
    public void vActApply() {
        ToastUtil.getInstance().showNormal(mContext, "报名成功");
    }

    @Override
    public void vActDetails(ActivityBean bean, String activityId) {
        ActivityDetailsActivity.startThis(mContext, bean, activityId,false);
    }

    @Override
    public void onSingUp(String activityId, String wechatNum) {
        mPresenter.pActApply(activityId, wechatNum);
    }

    @Override
    public void onQA() {
        WebActivity.startThis(mContext, "", Contract.QA_URL);
    }
}
